package com.xxg.renrenmessage.dao;

import com.mongodb.client.MongoCollection;
import com.mongodb.client.model.Indexes;
import com.xxg.renrencommon.domain.bean.MessageInfo;
import org.bson.Document;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Repository;

import java.util.List;

import static org.springframework.data.mongodb.core.query.Criteria.where;

/**
 * 类说明
 *
 * @author szj
 * @date 2021/12/6
 */
@Repository
public class MessageInfoDao {
    private final MongoTemplate mongoTemplate;

    public MessageInfoDao(MongoTemplate mongoTemplate) {
        this.mongoTemplate = mongoTemplate;
        createCollection();
    }

    /**
     * 创建collection
     */
    private void createCollection() {
        String collectionName = "messageInfo";
        MongoCollection<Document> collection;
        if (!mongoTemplate.getCollectionNames().contains(collectionName)) {
            collection = mongoTemplate.createCollection(MessageInfo.class);
            createIndex(collection);
        }
    }

    /**
     * 创建索引
     *
     * @param collection 数据集
     */
    private void createIndex(MongoCollection<Document> collection) {
        collection.createIndex(Indexes.ascending("senderId"));
        collection.createIndex(Indexes.ascending("recipientId"));
        collection.createIndex(Indexes.ascending("hasRead"));
        collection.createIndex(Indexes.ascending("type"));
        collection.createIndex(Indexes.compoundIndex(Indexes.ascending("recipientId"), Indexes.ascending("hasRead")));
        collection.createIndex(Indexes.compoundIndex(Indexes.ascending("recipientId"), Indexes.ascending("type")));
        collection.createIndex(Indexes.compoundIndex(Indexes.ascending("senderId"), Indexes.ascending("recipientId")));
    }

    public List<MessageInfo> findByRecipientIdAndType(int recipientId, Integer type, Integer pageNo, Integer pageSize) {
        Query query = new Query(where("recipientId").is(recipientId).and("type").is(type).and("isDelete").is(false));
        query.skip((pageNo - 1) * pageSize);
        query.limit(pageSize);
        query.with(Sort.by("createTime").descending());
        return mongoTemplate.find(query, MessageInfo.class);
    }

    public void setReadByIds(int recipientId, List<Integer> ids) {
        Query query = new Query(where("recipientId").is(recipientId).and("_id").in(ids));
        Update update = new Update();
        update.set("hasRead", true);
        mongoTemplate.updateMulti(query, update, MessageInfo.class);
    }

    public void setAllReadByType(int recipientId, Integer type) {
        Query query = new Query(where("recipientId").is(recipientId).and("type").is(type));
        Update update = new Update();
        update.set("hasRead", true);
        mongoTemplate.updateMulti(query, update, MessageInfo.class);
    }

    public void deleteByIds(int recipientId, List<Integer> ids) {
        Query query = new Query(where("recipientId").is(recipientId).and("_id").in(ids));
        Update update = new Update();
        update.set("isDelete", true);
        mongoTemplate.updateMulti(query, update, MessageInfo.class);
    }
}
